


// 性能监控视图页面
void Request_View_Performance(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		LoadPage(c, 200, HTTP_CT_HTML, "performance.html");
	} else {
		LoadPage(c, 404, HTTP_CT_HTML, "status/404.html");
	}
}


// 获取当前实时性能数据
void Request_Performance_Current(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 使用缓存的性能数据（由后台线程每30秒更新）
		PerformanceData data = G_LastPerfData;
		
		// 构建 JSON响应
		xvalue tblResult = xvoCreateTable();
		xvoTableSetBool(tblResult, "result", 6, TRUE);
		
		xvalue tblData = xvoCreateTable();
		xvoTableSetFloat(tblData, "cpu", 3, data.cpu);
		xvoTableSetFloat(tblData, "memory", 6, data.memory);
		xvoTableSetInt(tblData, "memory_total", 12, data.memory_total);
		xvoTableSetInt(tblData, "memory_used", 11, data.memory_used);
		xvoTableSetFloat(tblData, "network_tx", 10, data.network_tx);
		xvoTableSetFloat(tblData, "network_rx", 10, data.network_rx);
		xvoTableSetFloat(tblData, "disk_read", 9, data.disk_read);
		xvoTableSetFloat(tblData, "disk_write", 10, data.disk_write);
		xvoTableSetInt(tblData, "timestamp", 9, data.timestamp);
		
		xvoTableSetValue(tblResult, "data", 4, tblData, TRUE);
		
		size_t iSize = 0;
		str sJSON = xrtStringifyJSON(tblResult, FALSE, &iSize);
		http_reply(c, 200, HTTP_CT_JSON, sJSON, iSize);
		xvoUnref(tblResult);
		
	} else {
		http_reply(c, 404, HTTP_CT_JSON, "{\"result\": false, \"msg\": \"不支持的请求方法\"}", 0);
	}
}


// 获取磁盘空间信息
void Request_Performance_Disk(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 获取磁盘列表
		DiskInfo* disks = NULL;
		int count = GetDiskList(&disks);
		
		// 构建JSON响应
		xvalue tblResult = xvoCreateTable();
		xvoTableSetBool(tblResult, "result", 6, TRUE);
		
		xvalue arrData = xvoCreateArray();
		for (int i = 0; i < count; i++) {
			xvalue tblDisk = xvoCreateTable();
			xvoTableSetText(tblDisk, "mount", 5, disks[i].mount, 0, FALSE);
			xvoTableSetInt(tblDisk, "total", 5, disks[i].total);
			xvoTableSetInt(tblDisk, "used", 4, disks[i].used);
			xvoTableSetInt(tblDisk, "available", 9, disks[i].available);
			xvoArrayAppendValue(arrData, tblDisk, TRUE);
		}
		
		xvoTableSetValue(tblResult, "data", 4, arrData, TRUE);
		
		size_t iSize = 0;
		str sJSON = xrtStringifyJSON(tblResult, FALSE, &iSize);
		http_reply(c, 200, HTTP_CT_JSON, sJSON, iSize);
		xvoUnref(tblResult);
		
		// 释放磁盘信息
		if (disks) {
			for (int i = 0; i < count; i++) {
				xrtFree(disks[i].mount);
			}
			xrtFree(disks);
		}
		
	} else {
		http_reply(c, 404, HTTP_CT_JSON, "{\"result\": false, \"msg\": \"不支持的请求方法\"}", 0);
	}
}


// 获取历史性能数据
void Request_Performance_History(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 解析查询参数
		uint8 sPeriod[8];
		mg_http_get_var(&hm->query, "period", sPeriod, sizeof(sPeriod));
		
		// 计算时间范围
		xtime now = xrtNow();
		xtime startTime = now;
		int limit = 60;  // 默认返回60个数据点
		
		if (sPeriod && strcmp(sPeriod, "1h") == 0) {
			startTime = now - (1LL * 60 * 60 * 1000);  // 1小时
			limit = 60;
		} else if (sPeriod && strcmp(sPeriod, "6h") == 0) {
			startTime = now - (6LL * 60 * 60 * 1000);  // 6小时
			limit = 72;
		} else if (sPeriod && strcmp(sPeriod, "24h") == 0) {
			startTime = now - (24LL * 60 * 60 * 1000);  // 24小时
			limit = 96;
		} else if (sPeriod && strcmp(sPeriod, "7d") == 0) {
			startTime = now - (7LL * 24 * 60 * 60 * 1000);  // 7天
			limit = 168;
		} else if (sPeriod && strcmp(sPeriod, "30d") == 0) {
			startTime = now - (30LL * 24 * 60 * 60 * 1000);  // 30天
			limit = 720;
		} else {
			startTime = now - (1LL * 60 * 60 * 1000);  // 默认1小时
		}
		
		// 查询数据库
		str sSQL = xrtFormat(
			"SELECT cpu, memory, network_tx, network_rx, disk_read, disk_write, timestamp "
			"FROM performance WHERE timestamp >= %lld ORDER BY timestamp DESC LIMIT %d;",
			startTime, limit
		);
		
		XDO_Recordset rs = xdoSelect(G_DB, sSQL);
		xrtFree(sSQL);
		
		// 构建响应
		xvalue tblResult = xvoCreateTable();
		xvoTableSetBool(tblResult, "result", 6, TRUE);
		
		xvalue arrData = xvoCreateArray();
		while ( xrsNext(rs) ) {
			xvalue tblRow = xvoCreateTable();
			xvoTableSetFloat(tblRow, "cpu", 3, atof(xrsGetValue(rs, 0)));
			xvoTableSetFloat(tblRow, "memory", 6, atof(xrsGetValue(rs, 1)));
			xvoTableSetFloat(tblRow, "network_tx", 10, atof(xrsGetValue(rs, 2)));
			xvoTableSetFloat(tblRow, "network_rx", 10, atof(xrsGetValue(rs, 3)));
			xvoTableSetFloat(tblRow, "disk_read", 9, atof(xrsGetValue(rs, 4)));
			xvoTableSetFloat(tblRow, "disk_write", 10, atof(xrsGetValue(rs, 5)));
			xvoTableSetInt(tblRow, "timestamp", 9, xrtStrToI64(xrsGetValue(rs, 6)));
			xvoArrayAppendValue(arrData, tblRow, TRUE);
		}
		xrsFree(rs);
		
		xvoTableSetValue(tblResult, "data", 4, arrData, TRUE);
		
		size_t iSize = 0;
		str sJSON = xrtStringifyJSON(tblResult, FALSE, &iSize);
		http_reply(c, 200, HTTP_CT_JSON, sJSON, iSize);
		xrtFree(sJSON);
		xvoUnref(tblResult);
		
	} else {
		http_reply(c, 404, HTTP_CT_JSON, "{\"result\": false, \"msg\": \"不支持的请求方法\"}", 0);
	}
}


// 获取服务器探针信息
void Request_Performance_ServerInfo(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm)
{
	if ( hm->methodCode == HTTP_GET ) {
		
		// 获取服务器信息
		ServerInfo info = {0};
		GetServerInfo(&info);
		
		// 构建 JSON 响应
		xvalue tblResult = xvoCreateTable();
		xvoTableSetBool(tblResult, "result", 6, TRUE);
		
		xvalue tblData = xvoCreateTable();
		xvoTableSetText(tblData, "os_type", 7, info.os_type, 0, FALSE);
		xvoTableSetText(tblData, "os_version", 10, info.os_version, 0, FALSE);
		xvoTableSetText(tblData, "hostname", 8, info.hostname, 0, FALSE);
		xvoTableSetText(tblData, "cpu_model", 9, info.cpu_model, 0, FALSE);
		xvoTableSetInt(tblData, "cpu_cores", 9, info.cpu_cores);
		xvoTableSetInt(tblData, "uptime", 6, info.uptime);
		xvoTableSetText(tblData, "kernel_version", 14, info.kernel_version, 0, FALSE);
		xvoTableSetText(tblData, "architecture", 12, info.architecture, 0, FALSE);
		xvoTableSetText(tblData, "ip_address", 10, info.ip_address, 0, FALSE);
		xvoTableSetText(tblData, "mac_address", 11, info.mac_address, 0, FALSE);
		
		xvoTableSetValue(tblResult, "data", 4, tblData, TRUE);
		
		size_t iSize = 0;
		str sJSON = xrtStringifyJSON(tblResult, FALSE, &iSize);
		http_reply(c, 200, HTTP_CT_JSON, sJSON, iSize);
		xvoUnref(tblResult);
		
		// 释放服务器信息
		FreeServerInfo(&info);
		
	} else {
		http_reply(c, 404, HTTP_CT_JSON, "{\"result\": false, \"msg\": \"不支持的请求方法\"}", 0);
	}
}

